package Scrap;

public class demo26_PermutationString {

	public static void main(String[] args) {

		String s = "abc";

		theArray = new String[factorial(s.length())]; // create a string array
														// to keep perms
		displayString(s);

		System.out.println("Array now populated with " + c + " elements.");
		System.out.println("Displaying first 10 elements:");

		for (int i = 0; i < theArray.length; i++) {
			System.out.println(theArray[i]);
		}
	}

	public static void displayString(String s) {
		displayString("", s);
	}

	private static void displayString(String prefix, String suffix) {
		int N = suffix.length();
		if (N == 0) {
			System.out.println(prefix);
			theArray[c] = prefix;
			c++;
		} else {
			for (int i = 0; i < N; i++)
				displayString(prefix + suffix.charAt(i),
						suffix.substring(0, i) + suffix.substring(i + 1, N));
		}
	}

	public static int factorial(int n) {
		return (n == 1) ? 1 : n * factorial(n - 1);
	}

	private static int c = 0;
	private static String[] theArray;
}